/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2023-05-10     洪家团       the first version
 */
#include <rtthread.h>
#include <board.h>

#define PWM_SERVOS_NAME "pwm3" /* PWM设 备 名 称 */
#define PWM_SERVOS_CHANNEL 1 /* PWM通 道 */
#define PWM_SERVOS_PERIOD 20000000  //20ms

struct rt_device_pwm *servos_dev; /* PWM设 备 句 柄 */


/**
 * @brief    该函数用于初始化舵机
 * @note     如果要改输出通道，需要修改4个文件：board.h\drv_pwm.c\pwm_config.h\tim_config.h
 * @param    无传入的参数
 * @return   无返回值
 */
void servos_init()
{
    /* 查 找 设 备 */
    servos_dev = (struct rt_device_pwm *)rt_device_find(PWM_SERVOS_NAME);
    if (servos_dev == RT_NULL)
    {
        rt_kprintf("pwm sample run failed! can't find %s device!\n", PWM_SERVOS_NAME);
        return RT_ERROR;
    }

    /* 使 能 设 备 */
    rt_pwm_enable(servos_dev, PWM_SERVOS_CHANNEL);

}


/**
 * @brief    该函数用于舵机运动
 * @note     舵机周期被设置成20ms，也就是500000
 * @param    传入的参数为pulse,32位无符号整形，用于调整占空比
 *                               传入的参数:
 *               500000          0.5ms占空比，0度
 *               2500000         2.5ms占空比，180度
 * @return   无返回值
 */
void servos_turn(rt_uint32_t pulse)
{
    rt_pwm_set(servos_dev, PWM_SERVOS_CHANNEL, PWM_SERVOS_PERIOD, pulse);
}



